package org.black.missedCall2.reply.contacts;

import java.util.ArrayList;
import java.util.List;

import org.black.missedCall2.MissedCallConstant;
import org.black.missedCall2.R;
import org.black.missedCall2.reply.ReplyConstant;
import org.black.missedCall2.reply.contacts.dataMOdel.ContactDataModel;
import org.black.missedCall2.reply.contacts.dataMOdel.ContactListAdapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

public class ContactListActivity extends Activity {
	private String editorFor = null;

	public void onCreate(Bundle savedInstanceState) {
		if (this.getIntent() != null) {
			Bundle bundle = this.getIntent().getExtras();
			this.editorFor = bundle
					.getString(ReplyConstant.INTENT_SMS_EDITOR_FOR);
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_list);
		List<ContactDataModel> contactDataModels = new ArrayList<ContactDataModel>();

		ListView contactListListView = (ListView) this
				.findViewById(R.id.contactListListView);

		contactListListView.setItemsCanFocus(true);
		contactListListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		// contactListListView.setEmptyView(noMissedCallTextView);
		contactListListView.setDivider(new ColorDrawable(Color.WHITE));
		contactListListView.setDividerHeight(1);

		Log.i(MissedCallConstant.APP_TAG, System.currentTimeMillis() + "");
		String sortOrder = PhoneLookup.DISPLAY_NAME + " ASC";
		Cursor contactCursor = null;
		contactCursor = getContentResolver()
				.query(ContactsContract.Data.CONTENT_URI,
						new String[] {
								ContactsContract.Data._ID,
								ContactsContract.Data.CONTACT_ID,
								ContactsContract.Contacts.HAS_PHONE_NUMBER,
								ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
								ContactsContract.CommonDataKinds.Phone.NUMBER,
								ContactsContract.CommonDataKinds.Photo.PHOTO_ID },
						ContactsContract.Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
								+ "'", null, sortOrder);
		if (contactCursor.moveToFirst() == true) {
			do {
				if (contactCursor
						.getInt(contactCursor
								.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
					ContactDataModel contactDataModel = new ContactDataModel();
					contactDataModel.setDataId(contactCursor.getLong(contactCursor
							.getColumnIndex(ContactsContract.Data._ID)));
					contactDataModel
							.setName(contactCursor.getString(contactCursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
					contactDataModel
							.setPhoneNumber(contactCursor.getString(contactCursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

					if (TextUtils
							.isEmpty(contactCursor.getString(contactCursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_ID))) == false) {
						contactDataModel
								.setContactId(contactCursor.getLong(contactCursor
										.getColumnIndex(ContactsContract.Data.CONTACT_ID)));
					}
					contactDataModels.add(contactDataModel);

				}
			} while (contactCursor.moveToNext());
		}
		contactCursor.close();

		Log.i(MissedCallConstant.APP_TAG, System.currentTimeMillis() + "");
		ContactListAdapter contactListAdapter = new ContactListAdapter(this,
				contactDataModels);
		contactListListView.setAdapter(contactListAdapter);
		contactListListView.setItemsCanFocus(false);
		contactListListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						ListView listView = (ListView) parent;
						Object object = listView.getItemAtPosition(position);
						if (object instanceof ContactDataModel) {
							Intent result = new Intent();
							Bundle bundle = new Bundle();
							bundle.putSerializable(
									ReplyConstant.BUNDLE_KEY_SELECTED_CONTACT,
									(ContactDataModel) object);
							bundle.putString(
									ReplyConstant.INTENT_SMS_EDITOR_FOR,
									editorFor);
							result.putExtras(bundle);
							// result.putExtra(ReplyConstant.INTENT_SMS_EDITOR_FOR,
							// editorFor);
							setResult(ReplyConstant.CODE_GET_CONTACT_INFO,
									result);
						}
						finish();
						// TODO Auto-generated method stub

					}
				});

		Button contactListCancelButton = (Button) findViewById(R.id.contactListCancelButton);
		contactListCancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
}
